<?php
/**
 * 
 * @authors Gary_zhou
 * @date    2016-06-29 12:49:40
 * @version v1.0.0
 */

require ('./xml2array.php');

//var_dump($_POST);

$post_data = array(
	array($_POST['url'],$_POST['title'],$_POST['desc'],$_POST['shareimg'],$_POST['shareurl']),
);

if (count($post_data)<=0) {

	echo false;

	return false;
}

$tempSet = array();

foreach ($post_data as $k => $v) {
	$tempSet = [];

	$v[0] = isset($v[0])?$v[0]:'';
	$v[1] = isset($v[1])?$v[1]:'';
	$v[2] = isset($v[2])?$v[2]:'';
	$v[3] = isset($v[3])?$v[3]:'';
	$v[4] = isset($v[4])?$v[4]:'';

	if (!$v[0]||!$v[1]||!$v[2]||!$v[3]) {
		continue;
	}
	
	$tempSet[] = $v[0];
	$tempSet[] = $v[1];
	$tempSet[] = $v[2];
	$tempSet[] = $v[3];
	$tempSet[] = $v[4];

	$dataArray[] = $tempSet;
}

//var_dump($dataArray);

$xml_start = '<?xml version="1.0" encoding="UTF-8"?>
<shares>';

$xml_end = '</shares>';

$xml_tpl = "";

foreach ($dataArray as $k => $v) {
	$v[0] = isset($v[0])?$v[0]:'';
	$v[1] = isset($v[1])?$v[1]:'';
	$v[2] = isset($v[2])?$v[2]:'';
	$v[3] = isset($v[3])?$v[3]:'';
	$v[4] = isset($v[4])?$v[4]:'';

	$xml_tpl .= "
		<share id='".$k."'>
			<url>$v[0]</url>
			<title>$v[1]</title>
			<desc>$v[2]</desc>
			<shareimg>$v[3]</shareimg>
			<shareurl>$v[4]</shareurl>
		</share>
		";
}

$xml = $xml_start.$xml_tpl.$xml_end;

$share_xml = fopen("../db/wxShare.xml", "r+") or die("Unable to open file!");

fwrite($share_xml, $xml);

fclose($share_xml);

echo json_encode(array('code' => 0));